<?php
$mld = MLDnewsletter::getInstance();
global $HTMLEDITOR, $SITEURL;

//save data if submitted	
if (isset($_POST['savesettings'])) {

    //print_r($_POST);		
    if (isset($_POST['settings']['smtpauth']) && $_POST['settings']['smtpauth'] == 'on') {
        $SMTPAUTH2 = 'true';
    } else {
        $SMTPAUTH2 = 'false';
    }
    if (isset($_POST['settings']['usesitemail']) && $_POST['settings']['usesitemail'] == 'on') {
        $USESITEMAIL2 = 'true';
    } else {
        $USESITEMAIL2 = 'false';
    }
    $xmls = new SimpleXMLExtended('<settings></settings>');
    $note = $xmls->addChild('sname');
    $note->addCData($_POST['settings']['sname']);
    $note = $xmls->addChild('semail');
    $note->addCData($_POST['settings']['semail']);
    $note = $xmls->addChild('usesitemail', $USESITEMAIL2);
    $note = $xmls->addChild('smtpserver', $_POST['settings']['smtpserver']);
    $note = $xmls->addChild('smtpport', (int) $_POST['settings']['smtpport']);
    $note = $xmls->addChild('smtpauth', $SMTPAUTH2);
    $note = $xmls->addChild('smtpsecure', $_POST['settings']['smtpsecure']);
    $note = $xmls->addChild('smtpauthname', $_POST['settings']['smtpauthname']);
    $note = $xmls->addChild('smtpauthpw', $_POST['settings']['smtpauthpw']);
    $note = $xmls->addChild('theader');
    $note->addCData(htmlentities($_POST['settings']['theader'], ENT_QUOTES, 'UTF-8'));
    $note = $xmls->addChild('tfooter');
    $note->addCData(htmlentities($_POST['settings']['tfooter'], ENT_QUOTES, 'UTF-8'));


    if (!XMLsave($xmls, $mld->files['settings'])) {
        $msg = array('error', i18n_r('mld-newsletter/SAVEFAIL'));
    } else {
        $msg = array('updated', i18n_r('mld-newsletter/SAVESUCCESS'));
    }
}

//get data from settingsfile
$data = $mld->getXmlData('settings');
$SENDERNAME = $data->sname;
$SENDEREMAIL = $data->semail;
$USESITEMAIL = ($data->usesitemail == 'true') ? 'checked' : '';
$SMTPSERVER = $data->smtpserver;
$SMTPPORT = $data->smtpport;
$SMTPAUTH = ($data->smtpauth == 'true') ? 'checked' : '';
$SMTPSECURE = $data->smtpsecure;
$SMTPAUTHNAME = $data->smtpauthname;
$SMTPAUTHPW = $data->smtpauthpw;    
$TEMPLATEHEADER = stripslashes(htmldecode($data->theader));
$TEMPLATEFOOTER = stripslashes(htmldecode($data->tfooter));

//show message
if (isset($msg)) {
    display_message($msg[0], $msg[1]);
}

if ($HTMLEDITOR != '') {
    if (defined('GSEDITORLANG')) {
        $EDLANG = GSEDITORLANG;
    } else {
        $EDLANG = 'en';
    }
}
?>
<h3><?php i18n('mld-newsletter/SETTINGS'); ?></h3>
<p><?php i18n('mld-newsletter/REQUIREDFIELDS'); ?></p>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post" class="mldsettingsform" >
<div class="leftsec">    
    <p><label class="mldlabel"><?php i18n('mld-newsletter/SENDERNAME'); ?>: *</label>
        <input type="text" class="text" name="settings[sname]" value="<?php echo $SENDERNAME; ?>"/></p>
</div><div class="rightsec">
        <p><label class="mldlabel"><?php i18n('mld-newsletter/SENDEREMAIL'); ?>: *</label>
        <input type="text" class="text" name="settings[semail]" value="<?php echo $SENDEREMAIL; ?>"/></p>
</div><div class="clear"></div>
    <p class="inline"><input type="checkbox" name="settings[usesitemail]" <?php echo $USESITEMAIL; ?> />
    <label class="mldlabel">&nbsp;<?php i18n('mld-newsletter/USESITEMAIL'); ?></label></p>    
<h3><?php i18n('mld-newsletter/SMTPSETTINGS'); ?></h3>
<div class="leftsec">
    <p><label class="mldlabel"><?php i18n('mld-newsletter/SMTPSERVER'); ?>: *</label>
    <input type="text" class="text" name="settings[smtpserver]" value="<?php echo $SMTPSERVER; ?>"/></p>
</div><div class="rightsec">
    <p><label class="mldlabel"><?php i18n('mld-newsletter/SMTPPORT'); ?>: *</label>
    <input type="text" class="text" name="settings[smtpport]" value="<?php echo $SMTPPORT; ?>"/></p>
</div><div class="clear"></div>
<div class="leftsec">
        <p class="inline"><input type="checkbox" name="settings[smtpauth]" <?php echo $SMTPAUTH; ?> />
    <label class="mldlabel">&nbsp;<?php i18n('mld-newsletter/SMTPAUTH'); ?></label></p>
</div><div class="rightsec">
    <p><label class="mldlabel"><?php i18n('mld-newsletter/SMTPSECURE'); ?>:</label>
    <input type="text" class="text" name="settings[smtpsecure]" value="<?php echo $SMTPSECURE; ?>"/></p>
</div><div class="clear"></div>
<div class="leftsec">
    <p><label class="mldlabel"><?php i18n('mld-newsletter/SMTPAUTHNAME'); ?>:</label>
    <input type="text" class="text" name="settings[smtpauthname]" value="<?php echo $SMTPAUTHNAME; ?>"/></p>
</div><div class="rightsec">
    <p><label class="mldlabel"><?php i18n('mld-newsletter/SMTPAUTHPW'); ?>:</label>
    <input type="password" class="text" name="settings[smtpauthpw]" value="<?php echo $SMTPAUTHPW; ?>"/></p>
</div><div class="clear"></div>
<h3><?php i18n('mld-newsletter/TEMPLATESETTINGS'); ?></h3>
    <p><label class="mldlabel"><?php i18n('mld-newsletter/TEMPLATEHEADER'); ?>:</label>
    <textarea class="mldtemplatepart" name="settings[theader]"><?php echo $TEMPLATEHEADER; ?></textarea></p>
    <p><label class="mldlabel"><?php i18n('mld-newsletter/TEMPLATEFOOTER'); ?>:</label>
    <textarea class="mldtemplatepart" name="settings[tfooter]"><?php echo $TEMPLATEFOOTER; ?></textarea></p>
    <p><input type="submit" class="submit" value="<?php i18n('mld-newsletter/SAVE_SETTINGS'); ?>" id="savesettings" name="savesettings" /></p>
</form>
<script type="text/javascript" src="template/js/ckeditor/ckeditor.js"></script>
        <script type="text/javascript">
                //<![CDATA[
                var editor = CKEDITOR.replace( 'settings[theader]', {
                skin : 'getsimple',
                forcePasteAsPlainText : true,
                language : '<?php echo $EDLANG; ?>',
                defaultLanguage : '<?php echo $EDLANG; ?>',
                entities : true,
                uiColor : '#FFFFFF',
                height: '250px',
                baseHref : '<?php echo $SITEURL; ?>',
                toolbar : [['Bold','Italic','Underline','JustifyLeft','JustifyCenter','JustifyRight','Table','TextColor','Link',
                        'Unlink','Image','Font','FontSize','Source']],
                filebrowserBrowseUrl : 'filebrowser.php?type=all',
                filebrowserImageBrowseUrl : 'filebrowser.php?type=images',
                filebrowserWindowWidth : '730',
                filebrowserWindowHeight : '500'
                });
                //]]>
        </script>
        <script type="text/javascript">
                //<![CDATA[
                var editor = CKEDITOR.replace( 'settings[tfooter]', {
                skin : 'getsimple',
                forcePasteAsPlainText : true,
                language : '<?php echo $EDLANG; ?>',
                defaultLanguage : '<?php echo $EDLANG; ?>',
                entities : true,
                uiColor : '#FFFFFF',
                height: '250px',
                baseHref : '<?php echo $SITEURL; ?>',
                toolbar : [['Bold','Italic','Underline','JustifyLeft','JustifyCenter','JustifyRight','Table','TextColor','Link',
                        'Unlink','Image','Font','FontSize','Source']],
                filebrowserBrowseUrl : 'filebrowser.php?type=all',
                filebrowserImageBrowseUrl : 'filebrowser.php?type=images',
                filebrowserWindowWidth : '730',
                filebrowserWindowHeight : '500'
                });
                //]]>
        </script>
